import apiClient from "../apiClient";

export interface Knowledge {
	id: number;
	name: string;
	url: string;
	icon: string;
	createtime: string | null;
	updatetime: string | null;
	status: number;
	status_text: string;
}

export interface KnowledgeQuery {
	page?: number;
	limit?: number;
	name?: string;
	status?: number;
}

export interface KnowledgeForm {
	name: string;
	url: string;
	status: number;
}

enum Api {
	KnowledgeList = "/knowledge/index",
	KnowledgeAdd = "/knowledge/add",
	KnowledgeEdit = "/knowledge/edit",
	KnowledgeDelete = "/knowledge/del",
}

export class KnowledgeService {
	/**
	 * 获取知识列表
	 */
	async knowledgeList(params: KnowledgeQuery) {
		return apiClient.get<Knowledge[]>({
			url: Api.KnowledgeList,
			params,
		});
	}

	/**
	 * 添加知识
	 */
	async knowledgeAdd(data: KnowledgeForm) {
		return apiClient.post<void>({
			url: Api.KnowledgeAdd,
			data,
		});
	}

	/**
	 * 编辑知识
	 */
	async knowledgeEdit(id: number, data: Partial<KnowledgeForm>) {
		return apiClient.post<void>({
			url: Api.KnowledgeEdit,
			data: { id, ...data },
		});
	}

	/**
	 * 删除知识
	 */
	async knowledgeDelete(id: number) {
		return apiClient.post<void>({
			url: Api.KnowledgeDelete,
			data: { id },
		});
	}

	/**
	 * 更新知识状态
	 */
	async knowledgeChangeStatus(id: number, status: number) {
		return this.knowledgeEdit(id, { status });
	}
}

export const knowledgeService = new KnowledgeService();
